package com.cskaoyan.gateway.controller.shopping;

import com.cskaoyan.gateway.form.shopping.Add2CartForm;
import com.cskaoyan.gateway.form.shopping.AllSelectForm;
import com.cskaoyan.gateway.form.shopping.UpdateCartForm;
import com.mall.commons.result.ResponseData;
import com.mall.commons.result.ResponseUtil;
import com.mall.shopping.ICartService;
import com.mall.shopping.dto.*;
import com.mall.user.constants.SysRetCodeConstants;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @Author: itsxslyn
 * @Date: 2021/9/28 23:25
 * @Description:
 */

@RestController
@RequestMapping("shopping")
public class CartsController {

    @Reference(timeout = 3000, retries = 0, check = false)
    ICartService iCartService;

    @GetMapping("carts")
    public ResponseData getCartsList(HttpServletRequest request) {

        String userId = (String) request.getAttribute("userInfo");
        CartListByIdRequest cartListByIdRequest = new CartListByIdRequest();
        cartListByIdRequest.setUserId(Long.parseLong(userId));
        CartListByIdResponse response = iCartService.getCartListById(cartListByIdRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getCartProductDtos());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @PostMapping("carts")
    public ResponseData addToCart(@RequestBody Add2CartForm add2Cart) {
        AddCartRequest addCartRequest = new AddCartRequest();
        addCartRequest.setUserId(add2Cart.getUserId());
        addCartRequest.setItemId(add2Cart.getProductId());
        addCartRequest.setNum(add2Cart.getProductNum());
        AddCartResponse response = iCartService.addToCart(addCartRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @PutMapping("carts")
    public ResponseData updateCart(@RequestBody UpdateCartForm updateCartForm) {
        UpdateCartNumRequest request = new UpdateCartNumRequest();
        request.setUserId(updateCartForm.getUserId());
        request.setItemId(updateCartForm.getProductId());
        request.setNum(updateCartForm.getProductNum());
        request.setChecked(updateCartForm.getChecked());
        UpdateCartNumResponse response = iCartService.updateCartNum(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @DeleteMapping("carts/{uid}/{pid}")
    public ResponseData deleleCart(@PathVariable("uid") Long uid, @PathVariable("pid") Long pid) {
        DeleteCartItemRequest request = new DeleteCartItemRequest();
        request.setItemId(pid);
        request.setUserId(uid);
        DeleteCartItemResponse response = iCartService.deleteCartItem(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @DeleteMapping("items/{uid}")
    public ResponseData deleteCheckCart(@PathVariable("uid") Long uid) {
        DeleteCheckedItemRequest request = new DeleteCheckedItemRequest();
        request.setUserId(uid);
        DeleteCheckedItemResponse response = iCartService.deleteCheckedItem(request);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }

    @PutMapping("items")
    public ResponseData allSelect(@RequestBody AllSelectForm allSelectForm) {
        CheckAllItemRequest checkAllItemRequest = new CheckAllItemRequest();
        checkAllItemRequest.setChecked(allSelectForm.getChecked());
        checkAllItemRequest.setUserId(allSelectForm.getUserId());

        CheckAllItemResponse response = iCartService.checkAllCartItem(checkAllItemRequest);
        if (response.getCode().equals(SysRetCodeConstants.SUCCESS.getCode())) {
            return new ResponseUtil<>().setData(response.getMsg());
        }
        return new ResponseUtil<>().setErrorMsg(response.getCode());
    }
}
